﻿using System;
using System.Xml.Serialization;
using INdTFramework.Extensions;
using RestSharp;

namespace INdTFramework.Http
{
    public class DefaultXMLHttpRequest<T, E> : HttpRestRequest<T, E>
    {
        public DefaultXMLHttpRequest(String resource, RestSharp.Method method, Action<T> successResponse, Action<E> errorResponse)
            : base(resource, method, successResponse, errorResponse) {}

        protected override void ProcessSuccessRequest(IRestResponse response, RestRequestAsyncHandle handle)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            _successResponse (serializer.DeserializeFromString<T> (response.Content));
        }

        protected override void ProcessFailedRequest(IRestResponse response, RestRequestAsyncHandle handle)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(E));
            _errorResponse(serializer.DeserializeFromString<E>(response.Content));
        }
    }
}
